package treeStudy;

public class DiameterOfBinaryTree {

    private int ans = 0;

    /**
     * LeetCode T543 二叉树的直径
     * @param args
     */
    public static void main(String[] args) {

    }

    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return ans;
    }

    public int dfs(TreeNode root){
        if (root == null){
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        ans = Math.max(left + right, ans);
        return 1 + Math.max(left, right);
    }
}
